//
//  YRJSearchResultDiscountTableViewCell.m
//  YanRenjie
//
//  Created by dllo on 15/11/4.
//  Copyright (c) 2015年 yanrenjie. All rights reserved.
//

#import "YRJSearchResultDiscountTableViewCell.h"
#import "YRJTripHeader.h"

//#define WIDTH self.contentView.frame.size.width
//#define HEIGHT self.contentView.frame.size.height
@implementation YRJSearchResultDiscountTableViewCell
- (void)dealloc {
    [_myCollectionView release];
    [_myFlowLayout release];
    [_tempArray release];
    
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createCell];
    }
    return self;
}

- (void)createCell {
    NSLog(@"%f", kHeight);
    NSLog(@"%f", kWidth);
    self.myFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.myFlowLayout.itemSize = CGSizeMake(SWIDTH / 2 - SWIDTH / 32 + SWIDTH / 128, (200.0 / 667) * SHEIGHT);
    self.myFlowLayout.sectionInset = UIEdgeInsetsMake((5.0 / 375) * SWIDTH, (5.0 / 667) * SHEIGHT, (5.0 / 375) * SHEIGHT, (5.0 / 667) * SHEIGHT);
    self.myFlowLayout.minimumInteritemSpacing = (5.0 / 375) * SWIDTH;
    self.myFlowLayout.minimumLineSpacing = (5.0 / 667) * SHEIGHT;
    
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height) collectionViewLayout:self.myFlowLayout];
    self.myCollectionView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.myCollectionView];
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    self.myCollectionView.scrollEnabled = YES;
    [_myFlowLayout release];
    [_myCollectionView release];
    
    [self.myCollectionView registerClass:[YRJSearchResultDiscountCollectionViewCell class] forCellWithReuseIdentifier:@"searchResultDiscountCollectionReuse"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSLog(@"self.tempArray.count-------------->%ld", self.tempArray.count);
    return self.tempArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    YRJSearchResultDiscountCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"searchResultDiscountCollectionReuse" forIndexPath:indexPath];
    YRJSearchResultDiscountModel *tempModel = self.tempArray[indexPath.row];
    cell.titleLabel.text = tempModel.title;
    // 关键字高亮
    NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:cell.titleLabel.text];
    [attribute addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:[cell.titleLabel.text rangeOfString:self.searchResultString]];
    [cell.titleLabel setAttributedText:attribute];
    
    [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:tempModel.pic] placeholderImage:[UIImage imageNamed:@"placeHold.png"]];
    cell.priceLabel.text = [[tempModel.price stringByReplacingOccurrencesOfString:@"<em>" withString:@""] stringByReplacingOccurrencesOfString:@"</em>" withString:@""];
    cell.lastminute_desLabel.text = tempModel.lastminute_des;
    cell.departureTimeLabel.text = tempModel.departureTime;
    NSLog(@"------>%@", tempModel.title);
    return cell;
}

// 重写tempArray的Setter方法, 再次进行collectionView的刷新
- (void)setTempArray:(NSMutableArray *)tempArray {
    if (_tempArray != tempArray) {
        _tempArray = tempArray;
    }
    [self.myCollectionView reloadData];
}

#pragma mark - collectionView的点击方法中YRJSearchResultDiscountTableViewCellDelegate 的协议方法调用
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    YRJSearchResultDiscountModel *model = self.tempArray[indexPath.row];
    [self.delegate pushToSearchResultDiscountWebViewController:model.url];
}




- (void)awakeFromNib {

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    NSLog(@"--*********-------**********--------->%ld", self.tempArray.count);
    self.myCollectionView.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height);
}

@end
